<?php
namespace app\admin\controller;

use think\facade\Db;
use think\facade\View;
class UserLevel extends Base
{
    public function index(){
        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            if(input('user_id')){
                $map['l.user_id'] = input('user_id');
            }
            if(input('username')){
                $map['u.username|u.mobile'] = input('username');
            }
            $count = Db::name('user_level')->alias('l')->where($map)->count();

            $field = '*';
            $list = Db::name('user_level')
                ->field($field)
                ->where($map)
                ->order('level_id desc ')
                ->page($page,$limit)
                ->select();
            foreach($list as $k=>$v){
                $intro_desc = '一级：'.($v['intro_bili1']).'%，二级：'.($v['intro_bili2']).'%，三级：'.($v['intro_bili3']).'%';
                $v['intro_desc'] = $intro_desc;
                $v['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
                $list[$k] = $v;

            }
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list]);
        }
        return View::fetch();
    }
    public function edit(){
        $id = input('id');
        $info = Db::name('user_level')->where('level_id',$id)->find();
        if($info){
            $info['w_time'] = date('Y-m-d H:i:s',$info['w_time']);
        }
        View::assign('info',$info);
        return View::fetch();
    }
    public function save(){
        $params = input();
        $id = input('id');
        $data = [];
        $data['level_name'] = $params['level_name'];
        $data['rank'] = $params['rank'];
        $data['describe'] = $params['desc'];
        $data['up_money'] = $params['up_money'];
        $data['intro_bili1'] = $params['intro_bili1'];
        $data['intro_bili2'] = $params['intro_bili2'];
        $data['intro_bili3'] = $params['intro_bili3'];
        $data['w_time'] = time();
        if($id){
            Db::name('user_level')->where('level_id',$id)->update($data);
        }else{
            Db::name('user_level')->insert($data);
        }
        return json(['status'=>true,'msg'=>'保存成功！']);
    }
    public function del(){
        $id_arr = input('ids/a');
        Db::startTrans();
        Db::name('user_level')->where(['id'=>['in',$id_arr]])->delete();
        Db::commit();
        return json(['status'=>1,'msg'=>'删除成功!']);
    }
    public function excel(){
        $title = [];

        $map = [];
        if(input('user_id')){
            $map['l.user_id'] = input('user_id');
        }
        if(input('username')){
            $map['u.username|u.mobile'] = input('username');
        }
        $count = Db::name('user_level')->alias('l')->where($map)->count();

        $field = '*';
        $list = Db::name('user_level')
            ->field($field)
            ->where($map)
            ->order('level_id desc ')
            ->select();
        $data = array();
        foreach($list as $k=>$vo){
            $data[] = array($vo['id'],$vo['headimg'],$vo['nickname'],$vo['platform'],$vo['pmember'],$vo['province'],$vo['city'],$vo['realname'],$vo['tel'],$vo['money'],$vo['score'],$vo['commission'],$vo['levelname'],date('Y-m-d H:i',$vo['createtime']),$vo['subscribe'],$vo['mpopenid'],$vo['wxopenid']);
        }
        $this->export_excel($title,$data);
    }
}
